﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Security.Cryptography;
using System.IO;

namespace Net.Class
{
    class NetCommon
    {
        public static string userName;
        public static string passWord;
        //public static string firstURL="http://www.baidu.com";

        // 除去所有html元素标记
        public static string striphtml(string strhtml)
        {
            string stroutput = strhtml;
            Regex regex = new Regex(@"<[^>]+>|</[^>]+>");

            stroutput = regex.Replace(stroutput, "");

            regex = new Regex("&nbsp;");
            stroutput = regex.Replace(stroutput, "");

            regex = new Regex(@"\ ");
            stroutput = regex.Replace(stroutput, "");
            return stroutput;

        }
        //去掉空行
        public static string removeBlankLines(string str)
        {
            str = System.Text.RegularExpressions.Regex.Replace(str, "(\r\n[\r|\n|\t| ]*\r\n)|(\n[\r|\n|\t| ]*\n)", "\r\n");
            return str;
        }
        //得到两个字符串之间的字符
        public static String getStringBetween(String content, String beginString, String endString)
        {
            if (beginString.Length <= 0 || endString.Length <= 0||content.Length<=0) return "";
            if (content.LastIndexOf(beginString) < 0 || content.IndexOf(endString) < 0) return "";
            return content.Substring(content.LastIndexOf(beginString) + beginString.Length, content.IndexOf(endString) - content.LastIndexOf(beginString) - beginString.Length).Trim();
        }

        public static string getSubStringToEnd(string content,string beginString)
        {
            return content.Substring(content.IndexOf(beginString) + beginString.Length).Trim();
        }

        public  const int TYPE_NOT_NULL = 0;
        public  const int TYPE_NUM = 1;
        public static bool isInputValid(String input, int type)
        {
            bool isValid = true;
            switch (type)
            {
                case 0://检查是否为空或长度为0
                    if (input == null || "".Equals(input))
                    {
                        isValid = false;
                    }
                    break;
                case 1://检查是否为正整数,用到时再写……
                    isValid = IsNumeric(input);//Regex.IsMatch(input, @"^\d*$"); 
                    break;
            }
            return isValid;

        }

        /**/
        /// <summary>
        /// 判断一个字符串是否为合法数字(指定整数位数和小数位数)
        /// </summary>
        /// <param name="s">字符串</param>
        /// <param name="precision">整数位数</param>
        /// <param name="scale">小数位数</param>
        /// <returns></returns>

        public static bool IsNumber(string s, int precision, int scale)
        {
            if ((precision == 0) && (scale == 0))
            {
                return false;
            }
            string pattern = @"(^\d{1," + precision + "}";
            if (scale > 0)
            {
                pattern += @"\.\d{0," + scale + "}$)|" + pattern;
            }
            pattern += "$)";
            return Regex.IsMatch(s, pattern);
        }

        ///<summary>
        ///验证输入的数据是不是正整数
        ///</summary>
        ///<param name="str">传入字符串</param>
        ///<returns>返回true或者false</returns>
        static bool IsNumeric(string str)
        {
            System.Text.RegularExpressions.Regex reg1 = new System.Text.RegularExpressions.Regex(@"^[0-9]\d*$");
            return reg1.IsMatch(str);
        }

        ////加密：     
        //static String Encrypt(String pwd) {     
        //    DESCryptoServiceProvider desc = new DESCryptoServiceProvider();//des进行加密     
        //    PasswordDeriveBytes db = new PasswordDeriveBytes(pwd, null);//产生key     
        //    byte[] key = db.GetBytes(8);     
        //    MemoryStream ms = new MemoryStream();//存储加密后的数据     
        //    CryptoStream cs = new CryptoStream(ms,desc.CreateEncryptor(key, key),CryptoStreamMode.Write);     
        //    byte[] data = Encoding.Unicode.GetBytes(pwd);//取到密码的字节流     
        //    cs.Write(data, 0, data.Length);//进行加密     
        //    cs.FlushFinalBlock();     
        //    byte[] res = ms.ToArray();//取加密后的数据     
        //    return Encoding.Unicode.GetString(res);//转换到字符串返回     
        //} 
        ////解密：     
        //static String Decrypt(String pwd, String data) {     
        //    DESCryptoServiceProvider desc = new DESCryptoServiceProvider();     
        //    PasswordDeriveBytes db = new PasswordDeriveBytes(pwd, null);//产生key     
        //    byte[] key = db.GetBytes(8);     
        //    MemoryStream ms = new MemoryStream();//存储解密后的数据     
        //    CryptoStream cs = new CryptoStream(ms,desc.CreateDecryptor(key, key),CryptoStreamMode.Write);     
        //    byte[] databytes = Encoding.Unicode.GetBytes(data);//取到加密后的数据的字节流     
        //    cs.Write(databytes, 0, databytes.Length);//解密数据     
        //    cs.FlushFinalBlock();     
        //    byte[] res = ms.ToArray();     
        //    return Encoding.Unicode.GetString(res);//返回解密后的数据     
        //}       


    }
}
